package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.CourseReply;
import com.czh.springboot04.service.CourseReplyService;
import com.czh.springboot04.service.serviceimpl.CourseReplyServiceImpl;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.List;

@CrossOrigin
@RequestMapping("CourseReply")
@RestController
public class CourseReplyController {

    @Autowired
    DataSource dataSource;
    @Autowired
    CourseReplyService courseReplyService = new CourseReplyServiceImpl();

    @PostMapping("insertCourseReply")
    public R insertCourseReply(@RequestBody CourseReply courseReply) {
        int i = courseReplyService.insertCourseReply(courseReply);
        if (i != 0) {
            return R.ok("回复成功");
        } else {
            return R.fail("回复失败");
        }
    }

    @PutMapping("updateCourseReplyById")
    public R updateCourseReplyById(@RequestBody CourseReply courseReply) {
        int i = courseReplyService.updateCourseReplyById(courseReply);
        if (i != 0) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @GetMapping("selectCourseReplyById")
    public R selectCourseReplyById(Integer id) {
        CourseReply courseReply = courseReplyService.selectCourseReplyById(id);
        if (courseReply != null) {
            return R.ok(courseReply);
        } else {
            return R.fail("查询失败");
        }
    }

    @DeleteMapping("deleteCourseReplyById")
    public R deleteCourseReplyById(Integer id) {
        int i = courseReplyService.deleteCourseReplyById(id);
        if (i != 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @DeleteMapping("deleteCourseReplyByList")
    public R deleteCourseReplyByList(@RequestBody List<Integer> idList) {
        int i = courseReplyService.deleteCourseReplyByList(idList);
        if (i != 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @GetMapping("selectCourseReplyAll")
    public R selectCourseReplyAll(String nickName, String title, int pageNum, int pageSize) {
        PageInfo<CourseReply> pageInfo = courseReplyService.selectCourseReplyAll(nickName, title, pageNum, pageSize);
        if (pageInfo != null) {
            return R.ok(pageInfo);
        } else {
            return R.fail("查询失败");
        }
    }
}
